<?php

session_start();
include("load-settings.php");

if(!isset($_SESSION['user']))
	header("Location: login.php");

$user = mysql_real_escape_string($_SESSION['user']);
$result = mysql_query("SELECT * FROM user WHERE id = $user") or die(mysql_error());
$row = mysql_fetch_array($result);

if($row['type'] != 2)
	header("Location: home.php");

$paypal = mysql_real_escape_string($_POST['paypal']);
$summoner = mysql_real_escape_string($_POST['summoner']);

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$region = mysql_real_escape_string($_POST['region']);
$goal = mysql_real_escape_string($_POST['goal']);
$start_tier = mysql_real_escape_string($_POST['start_tier']);
$start_division = mysql_real_escape_string($_POST['start_division']);
$end_tier = mysql_real_escape_string($_POST['end_tier']);
$end_division = mysql_real_escape_string($_POST['end_division']);
$payout = mysql_real_escape_string($_POST['payout']);
$booster_payout = mysql_real_escape_string($_POST['booster_payout']);


require_once("unirest/lib/Unirest.php");

$newresponse = Unirest::get(
  "https://teemojson.p.mashape.com/player/".$region."/".str_replace(" ", "", $summoner)."/leagues",
  array(
	"X-Mashape-Authorization" => "l5ekV2qGSVYpEWLviXHg4A6oRHRtqNBa"
  ),
  null
);

$decode = json_decode($newresponse->raw_body)->data->summonerLeagues;

$beginning_lp = 0;
$newwins = 0;
$newlosses = 0;
//echo "<pre>"; var_dump($newresponse); echo "</pre>"; die;
$x = 0;
while(isset($decode->array[$x]))
{
//$newdecode = json_decode($newresponse->raw_body)->data->summonerLeagues->array[0]->entries->array;
$newdecode = $decode->array[$x]->entries->array;
//echo "<pre>"; var_dump($newdecode); echo "</pre>"; die;

$y = 0;
while(isset($newdecode[$y]))
{
	if(strtolower($newdecode[$y]->playerOrTeamName) == strtolower($summoner))
	{
		$newwins = $newdecode[$y]->wins;
		$newlosses = $newdecode[$y]->losses;
		$beginning_lp = $newdecode[$y]->leaguePoints;
		break;
	}

	$y++;
}
$x++;
}

$beginning_stats = $newwins." wins, ".$newlosses." losses";

$result = mysql_query("SELECT * FROM user WHERE paypal = '$paypal'") or die(mysql_error());

if(mysql_num_rows($result) == 0)
{
    var_dump($_POST);die;
	$to = $paypal;
	$subject = 'ELO Coach Boosting';
	$message = str_replace("[password]", $password, $signup_message);
	$message = str_replace("\n", "\r\n", $message);

	$headers = 'From: Admin@VeteranBoosters.com' . "\r\n" .
		'Reply-To: Admin@VeteranBoosters.com' . "\r\n" .
		'X-Mailer: PHP/' . phpversion();

	mail($to, $subject, $message, $headers);

	$hash = crypt($password);

	mysql_query("INSERT INTO user (paypal, summoner, username, password, region, type) VALUES ('$paypal', '$summoner', '$username', '$hash', '$region', 0)") or die(mysql_error());

	$user = mysql_insert_id();
}
else
{
	$row = mysql_fetch_array($result);
	$user = $row['id'];

	$to = $paypal;
	$subject = 'ELO Coach Boosting';
	$message = str_replace("[password]", $password, $new_order_message);
	$message = str_replace("\n", "\r\n", $message);

	$headers = 'From: Admin@VeteranBoosters.com' . "\r\n" .
		'Reply-To: Admin@VeteranBoosters.com' . "\r\n" .
		'X-Mailer: PHP/' . phpversion();

	mail($to, $subject, $message, $headers);
}

if($goal == "")
	$goal = 'null';
if($wins == "")
	$wins = 'null';
if($losses == "")
	$losses = 'null';
if($end_division == "")
	$end_division = 'null';

$orders = mysql_query("SELECT MAX(id) FROM order_record");
$order_count = mysql_fetch_array($orders);
$duos = mysql_query("SELECT MAX(id) FROM duo_order");
$duo_count = mysql_fetch_array($duos);
$leveling = mysql_query("SELECT MAX(id) FROM leveling_order");
$leveling_count = mysql_fetch_array($leveling);
$referral = mysql_query("SELECT MAX(id) FROM referral_order");
$referral_count = mysql_fetch_array($referral);
$account = mysql_query("SELECT MAX(id) FROM account_order");
$account_count = mysql_fetch_array($account);

$max_order = $order_count['MAX(id)'];
$max_duo = $duo_count['MAX(id)'];
$max_leveling = $leveling_count['MAX(id)'];
$max_referral = $referral_count['MAX(id)'];
$max_account = $acount_count['MAX(id)'];

$new_id = $max_order;

if($max_duo > $new_id)
	$new_id = $max_duo;
if($max_leveling > $new_id)
	$new_id = $max_leveling;
if($max_referral > $new_id)
	$new_id = $max_referral;
if($max_account > $new_id)
	$new_id = $max_account;


$new_id++;
$all_orders = $new_id;

if($start_tier != "Unranked")
{
	mysql_query("INSERT INTO order_record (id, user, number_of_wins, start_tier, start_division, end_tier, end_division, payout, booster_payout, summoner, username, password, region, timestamp, beginning_stats, beginning_lp) VALUES ($all_orders, $user, $goal, '$start_tier', $start_division, '$end_tier', $end_division, $payout, $booster_payout, '$summoner', '$username', '$password', '$region', ".time().", '$beginning_stats', $beginning_lp)") or die(mysql_error());
}
else
{
	$newresponse = Unirest::get(
	  "https://teemojson.p.mashape.com/player/".$region."/".str_replace(" ", "", $summoner)."/past_seasons",
	  array(
		"X-Mashape-Authorization" => "l5ekV2qGSVYpEWLviXHg4A6oRHRtqNBa"
	  ),
	  null
	);

	$decode = json_decode($newresponse->raw_body)->data->seasonThree;

	$previousTier = "UNRANKED";
	$previousTier = $decode;

	mysql_query("INSERT INTO order_record (id, user, number_of_wins, start_tier, start_division, end_tier, end_division, payout, booster_payout, summoner, username, password, region, timestamp, beginning_stats, beginning_lp, previous_tier) VALUES ($all_orders, $user, $goal, '$start_tier', $start_division, '$end_tier', $end_division, $payout, $booster_payout, '$summoner', '$username', '$password', '$region', ".time().", '$beginning_stats', $beginning_lp, '$previousTier')") or die(mysql_error());
}


$order = $all_orders;

$result = mysql_query("SELECT * FROM user WHERE type = 2");
$row = mysql_fetch_array($result);
$admin = $row['id'];

if($goal == 'null')
{
	$boost_message = mysql_real_escape_string($boost_message);
	mysql_query("INSERT INTO order_chat (user, timestamp, message, order_record) VALUES ($admin, ".time().", '$boost_message', $order)") or die(mysql_error());
}
else
{
	$per_win_message = mysql_real_escape_string($per_win_message);
	mysql_query("INSERT INTO order_chat (user, timestamp, message, order_record) VALUES ($admin, ".time().", '$per_win_message', $order)") or die(mysql_error());
}


header("Location: orders-admin.php");

?>